﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace TouchDriver
{
    public class Mouse
    {
        #region Import Dll
        [DllImport("user32.dll")]
        extern static bool SetCursorPos(int x, int y);

        [DllImport("user32.dll")]
        extern static void mouse_event(int mouseEventFlag, int incrementX, int incrementY, uint data, UIntPtr extraInfo);

        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        #endregion

        public static void Move(int x, int y)
        {
            SetCursorPos(x, y);
        }

        public static void LeftClick(int x, int y)
        {
            //        System.Windows.Forms.Application.DoEvents();
            SetCursorPos(x, y);
            System.Threading.Thread.Sleep(500);

            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, UIntPtr.Zero);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, UIntPtr.Zero);

            //     System.Windows.Forms.Application.DoEvents();
        }

        public static void DoubleClick(int x, int y)
        {
            //      System.Windows.Forms.Application.DoEvents();
            SetCursorPos(x, y);
            System.Threading.Thread.Sleep(500);

            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, UIntPtr.Zero);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, UIntPtr.Zero);
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, UIntPtr.Zero);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, UIntPtr.Zero);

            //    System.Windows.Forms.Application.DoEvents();
        }
    }
}
